<template>
	<view class="page">
		<!-- 头部 -->
		<!-- <topBar :backUrl="backUrl" title="我的处方">
			<view class="font14 font-0078ff"
			@click="goNext('/pagesA/prescript/newPrescript?cate='+cate+'&consultation_id='+consultation_id+'&familyId=' + familyId + '&doctorId=' + doctorId)">新建处方</view>
		</topBar> -->
		
		<u-navbar placeholder autoBack leftText="我的处方" rightText="新建处方" @rightClick="goNext('/pagesA/prescript/newPrescript?cate='+cate+'&consultation_id='+consultation_id+'&familyId=' + familyId + '&doctorId=' + doctorId)"></u-navbar>
		<!-- 筛选 -->
		<view class="filter-bar flex-row-between">
			<block v-for="(item, index) in filterBar" :key="index">
				<view class="item" :class="{'active' : filterCurrent == item.id}" @click="filterList(item.id)">{{item.title}}</view>
			</block>
		</view>
		<!-- 标题 -->
		<view class="title flex-row-start">
			<view class="name font14">{{filterCurrent == 'listPrescription1' ? '常用' : (filterCurrent == 'listPrescription2' ? '公用' : '历史')}}处方列表({{count}})</view>
		</view>
		<!-- 列表 -->
		<view class="prescript-list">
			<LoadMore ref="LoadMore" id="LoadMore" :diffHeight="152" :initData="initData" @dataRefresh='dataRefresh' @infoRefresh='infoRefresh'>
				<block v-for="(item, index) in dataList" :key="index">
					<view class="item" @click="goDetail(item)">
						<view v-if="filterCurrent != 'listPrescription3'" class="part1 flex-row-between">
							<view class="font14">{{item.name}}</view>
							<view class="font12 font-969696">共{{item.goods_count}}种药品</view>
						</view>
						<view class="part2">
							<view v-if="filterCurrent == 'listPrescription3'" class="personal font14">{{item.user_the_patient_info.name}}<text>{{item.user_the_patient_info.sex == '1' ? '男' : '女'}}</text><text>{{item.user_the_patient_info.birthday}}岁</text></view>
							<view class="name font14">临床诊断：{{item.diagnosis_proposal || item.diagnose}}</view>
							<!-- 此处最多展示2个药品 -->
							<block v-for="(drug, i) in item.goods_list" :key="i">
								<view v-if="i < 2" class="font12 font-969696">
									<text>{{drug.goods_name}} {{drug.spec_name}} *{{drug.number}}</text>
									<text v-if="i == 1">...</text>
								</view>
							</block>
						</view>
						<view class="part3 flex-row-end">
							<view v-if="filterCurrent == 'listPrescription1'" class="btn-small grey" @click.stop="del(item.id)">删除</view>
							<view class="btn-small green" @click.stop="onEdit(item)">使用该处方</view>
						</view>
						<uni-icons class="icon" type="arrowright" size="14" color="#969696"></uni-icons>
					</view>
				</block>
			</LoadMore>
		</view>
	</view>
</template>

<script>
	import LoadMore from "@/components/loadMore.vue";
	import topBar from "@/components/ComTopBar.vue";
	export default {
		components: { LoadMore, topBar },
		data() {
			return {
				backUrl: 'back',
				familyId: '', // 就诊人的id
				doctorId: '', // 医生id
				cate: '',
				consultation_id: '',
				filterBar: [
					{ id: 'listPrescription1', title: '常用处方'},
					{ id: 'listPrescription2', title: '公用处方'},
					{ id: 'listPrescription3', title: '历史处方'},
				],
				filterCurrent: 'listPrescription1', // 高亮的id
				//传参数据
				initData: {
					url: 'listPrescription1',
					param: {},
				},
				dataList:[],
				count: 0,
			}
		},
		onLoad(options) {
			this.familyId = options.familyId
			this.doctorId = options.doctorId
			this.cate = options.cate
			this.consultation_id = options.consultation_id

			// this.backUrl = '/pagesA/message/doctor?familyId=' + options.familyId + '&doctorId=' + options.doctorId
		},
		methods: {
			// 筛选列表
			filterList (id) {
				this.filterCurrent = id
				this.initData.url = id
				this.$refs.LoadMore.oncutRefresh(this.initData)
			},
			// loadmore返回数据
			dataRefresh(e) {
				let $this = this
				e.forEach(item => {
					if (item.user_the_patient_info) {
						item.user_the_patient_info.birthday = $this.$base.Onage(item.user_the_patient_info.birthday)
					}
				})
				this.dataList = e
			},
			// loadmore返回信息
			infoRefresh(e) {
				this.count = e.count
			},
			// 删除处方
			del (id) {
				let that = this
				uni.showModal({
					title: '提示',
					confirmText: '确认',
					confirmColor: '#b8875c',
					cancelText: '取消',
					content: '您确定要删除该条处方吗?',
					success: function(res) {
						if (res.confirm) {
							that.$Http({
								url: 'deleteCommonlyUsedPrescription',
								show: true,
								data: {
									doctor_prescription_id: id
								},
							}).then(res => {
								if (res.code == 1) {
									that.$refs.LoadMore.oncutRefresh(that.initData)
								}
							});
						}
					}
				});
			},
			// 编辑(使用该处方)
			onEdit (item) {
				let type = ''
				if (this.filterCurrent == 'listPrescription3') {
					type = 'history'
				} else if (this.filterCurrent == 'listPrescription2') {
					type = 'public'
				} else {
					type = 'normal'
				}
				uni.navigateTo({
					url:  '/pagesA/prescript/newPrescript?cate='+this.cate+'&consultation_id='+this.consultation_id+'&familyId=' + this.familyId + '&doctorId=' + this.doctorId + '&prescriptId=' + item.id + '&type=' + type
				})
			},
			// 处方详情
			goDetail (item) {
				let type = ''
				if (this.filterCurrent == 'listPrescription3') {
					type = 'history'
				} else if (this.filterCurrent == 'listPrescription2') {
					type = 'public'
				} else {
					type = 'normal'
				}

				if (this.filterCurrent == 'listPrescription3') {
					uni.navigateTo({
						url: '/pagesA/prescript/historydetail?cate='+this.cate+'&consultation_id='+this.consultation_id+'&familyId=' + this.familyId + '&doctorId=' + this.doctorId + '&prescriptId=' + item.id + '&type=' + type
						// url: '/pagesA/prescript/historydetail?familyId=' + this.familyId + '&doctorId=' + this.doctorId + '&id=' + item.id
					})
				} else {
					uni.navigateTo({
						url: '/pagesA/prescript/oftenDetail?cate='+this.cate+'&consultation_id='+this.consultation_id+'&familyId=' + this.familyId + '&doctorId=' + this.doctorId + '&id=' + item.id + '&type=' + type
						// url: '/pagesA/prescript/oftenDetail?familyId=' + this.familyId + '&doctorId=' + this.doctorId + '&id=' + item.id
					})
				}
			},
			// 跳转
			goNext (url) {
				uni.navigateTo({
					url: url
				})
			}
		}
	}
</script>

<style lang="less">
	@import url("style.less");
	
	/deep/.u-navbar__content__right__text{
		color: #006EFF;
	}
</style>
